package org.jeecg.modules.job.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.job.rms.entity.RmsCompany;
import org.jeecg.modules.job.rms.entity.RmsCompanyPosition;
import org.jeecg.modules.job.rms.mapper.RmsCompanyMapper;
import org.jeecg.modules.job.rms.mapper.RmsCompanyPositionMapper;
import org.jeecg.modules.job.ums.entity.UmsCommunicate;
import org.jeecg.modules.job.ums.mapper.UmsApplyReadMapper;
import org.jeecg.modules.job.ums.mapper.UmsCommunicateMapper;
import org.jeecg.modules.job.ums.service.IUmsAccountRecordsService;
import org.jeecg.modules.job.ums.service.IUmsAccountService;
import org.jeecg.modules.job.ums.service.IUmsCommunicateService;
import org.jeecg.modules.job.ums.service.IUmsMemberRoleService;
import org.jeecg.modules.job.wms.entity.WmsJobExpect;
import org.jeecg.modules.job.wms.entity.WmsResume;
import org.jeecg.modules.job.wms.entity.WmsWorkExp;
import org.jeecg.modules.job.wms.mapper.WmsJobExpectMapper;
import org.jeecg.modules.job.wms.mapper.WmsResumeMapper;
import org.jeecg.modules.job.wms.mapper.WmsWorkExpMapper;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 沟通记录
 * @Author: jeecg-boot
 * @Date:   2022-12-18
 * @Version: V1.0
 */
@Service
public class UmsCommunicateServiceImpl extends ServiceImpl<UmsCommunicateMapper, UmsCommunicate> implements IUmsCommunicateService {

    @Resource
    private RmsCompanyPositionMapper companyPositionMapper;
    @Resource
    private WmsResumeMapper resumeMapper;
    @Resource
    private RmsCompanyMapper companyMapper;
    @Resource
    private WmsWorkExpMapper workExpMapper;
    @Resource
    private WmsJobExpectMapper jobExpectMapper;
    @Resource
    private IUmsMemberRoleService memberRoleService;
    @Resource
    private ISysBaseAPI sysBaseAPI;


    @Override
    public boolean addOrUpdate(UmsCommunicate param) {
        UmsCommunicate result=this.getOne(new QueryWrapper<>(param));
        if (result!=null){
            param.setId(result.getId());
            return  this.updateById(param);
        }else{
            return  this.save(param);
        }
    }

    @Override
    public IPage<UmsCommunicate> findPageBySpec(Page<UmsCommunicate> page, UmsCommunicate param) {
        IPage<UmsCommunicate> pageInfo = this.baseMapper.selectPage(page, new QueryWrapper<>(param));
        if(param.getType()==0){
            page.getRecords().forEach(item -> {
                if (item.getPositionId()!=null){
                    item.setPosition(companyPositionMapper.selectById(item.getPositionId()));
                }
                //招聘者信息
                LoginUser memberResult=sysBaseAPI.getUserById(item.getComMemberId());
                item.setMemberAvatar(memberResult.getAvatar());
                item.setMemberName(memberResult.getRealname());
                item.setMemberPostName(memberRoleService.getMemberRole(memberResult.getId()).getPostName());
                //企业信息
                RmsCompany company=companyMapper.selectById(item.getCompanyId());
                if (company!=null){
                    item.setCompanyName(company.getName());
                    item.setCompanyStaffSize(company.getStaffSize());
                    item.setCompanyNature(company.getCompanyNature());
                }
            });
        }else{
            page.getRecords().forEach(item -> {
                //求职者信息
                LoginUser memberResult=sysBaseAPI.getUserById(item.getMemberId());
                item.setMemberAvatar(memberResult.getAvatar());
                item.setMemberName(memberResult.getRealname());
                item.setMemberPostName(memberRoleService.getMemberRole(memberResult.getId()).getPostName());
                item.setMemberSex(memberResult.getSex()+"");
                //简历
                item.setResume(resumeMapper.selectOne(new QueryWrapper<>(new WmsResume().setMemberId(item.getMemberId()))));
                //工作经历
                List<WmsWorkExp> list=workExpMapper.selectList(new QueryWrapper<>(new WmsWorkExp().setMemberId(item.getMemberId())));
                if (list.size()>0){
                    item.setCompanyName(list.get(0).getCompanyName());
                    item.setCompanyPostName(list.get(0).getPostName());
                    item.setWorkSkill(list.get(0).getSkill());
                }
                //期望薪资
                List<WmsJobExpect> expectResults =jobExpectMapper.selectList(new QueryWrapper<>(new WmsJobExpect().setId(item.getMemberId())));
                if (expectResults.size()>0){
                    item.setExpectSalary(expectResults.get(0).getExpectSalary());
                }
            });
        }
        return pageInfo;
    }
}
